/**
 * @filename:JobController 2022-9-28
 * @project Wonder Java前后台分离框架  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.admin.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wonder.admin.domain.Job;
import com.wonder.admin.service.JobService;
import com.wonder.admin.request.JobRequest;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.config.WonderConfig;
import com.wonder.common.exception.job.TaskException;
import com.wonder.common.utils.Convert;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.ThreadLocalUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
/**   
 * 
 * <p>说明： 任务管理API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    2022-9-28
 *
 */
@Api(tags = "任务管理")
@RestController
@RequestMapping("/admin/Job")
public class JobController {
	
	@Autowired
	private JobService jobService;
	
	@ApiOperation(value = "任务管理列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody JobRequest jobRequest) {
		Page<Job> page = new Page<Job>(jobRequest.getPage(), jobRequest.getLimit());
		IPage<Job> list = jobService.selectList(jobRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   对象参数：Job
	 * @return  Boolean
	 * @author  Wonder
	 * @throws SchedulerException 
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Job> updateExt(@RequestBody Job entity) throws SchedulerException{
		if (null!=entity) {
			Job old=jobService.getById(entity.getJob_id());
			boolean rsg=false;
			if(old.getStatus()!=entity.getStatus()) {
				 rsg = jobService.changeStatus(entity);	
			}else {
			     rsg = jobService.updateById(entity);
			}
			if (rsg) {
				  return AjaxResult.success("system.success");
			  }else {
				  return AjaxResult.failed("system.fail");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   对象参数：Job
	 * @return  Boolean
	 * @author  Wonder
	 * @throws TaskException 
	 * @throws SchedulerException 
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Job> add(@RequestBody Job entity) throws SchedulerException, TaskException{

		if (null!=entity) {
            if(ThreadLocalUtil.get("id")!=null) {
		    	entity.setCreate_by(Long.parseLong(ThreadLocalUtil.get("id").toString()));
		    }
		    
		    String validate=jobService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = jobService.insertJob(entity)>0?true:false;
			if (rsg) {
				  return AjaxResult.success("system.success");
			  }else {
				  return AjaxResult.failed("system.fail");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取任务管理对象", notes = "作者：Wonder")
	public AjaxResult<Job> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) AjaxResult.failed("system.parameterError");
		Long job_id=Long.parseLong(map.get("job_id").toString());		
		 Job job=jobService.getById(job_id);
		
		if (null!=job ) {
			 return AjaxResult.success(job);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	/**
	 * @explain 修改
	 * @param   对象参数：Job
	 * @return  Boolean
	 * @author  Wonder
	 * @throws TaskException 
	 * @throws SchedulerException 
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Job> update(@RequestBody Job entity) throws SchedulerException, TaskException{
		if (null!=entity) {
		   if(ThreadLocalUtil.get("id")!=null) {
		    	entity.setUpdate_by(Long.parseLong(ThreadLocalUtil.get("id").toString()));
		    }
		    String validate=jobService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = jobService.updateJob(entity)>0?true:false;
			if (rsg) {
				  return AjaxResult.success("system.success");
			  }else {
				  return AjaxResult.failed("system.fail");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 删除
	 * @param   对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 * @throws SchedulerException 
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Job> delete(@RequestBody Map<String,Object> map) throws SchedulerException{
		if(map==null)return AjaxResult.failed("system.parameterError");
		String ids=map.get("job_id").toString();
        jobService.deleteJobByIds(ids);
	
		return AjaxResult.success("system.success");
	  
		
	}
	
	/**
	 * @explain 查看对象明细  
	 * @param   对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "获取任务管理对象明细", notes = "作者：Wonder")
	public AjaxResult<Job> detail(@RequestBody Map<String,Object> map){
	    if(map==null) AjaxResult.failed("system.parameterError");
		Long job_id=Long.parseLong(map.get("job_id").toString());		
		Job job=jobService.getById(job_id);
		if (null!=job ) {	
			 return AjaxResult.success(job);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	/**
	 * @explain 执行一次
	 * @param   对象参数：Job
	 * @return  Boolean
	 * @author  Wonder
	 * @throws SchedulerException 
	 * @throws TaskException 
	 */
	@PostMapping("/runOnce")
	@ApiOperation(value = "执行一次", notes = "作者：Wonder")
	public Map<String,Object> runOnce(@RequestBody Job entity) throws SchedulerException, TaskException{
		//Here we will wrote code for the service.
		 if(entity==null || entity.getJob_id()==null) AjaxResult.failed("system.parameterError");
		Job job=jobService.getById(entity.getJob_id());
		if(job.getModule().equals(WonderConfig.getModule()))
		{
          jobService.run(job);
          return AjaxMap.success();
		}else {
		 return AjaxMap.failed("system.noPermission");
		}
	}
		
	
}